<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-10-18
 * Time: 13:59
 */

namespace app\manage\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder; // 引入ZBuilder
use app\common\model\Notice as NoticModel;
use think\Db;

class Notic extends Admin
{
    //公告列表
    public function notic_list()
    {
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $data_list = NoticModel::where($map)->order($order)->paginate();
        // 分页数据
        $page = $data_list->render();
        $btn_access1 = [
            'title' => '公告编辑',
            'icon'  => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href'  => url('edit', ['id' => '__id__']),
        ];
        $btn_access = [
            'title' => '新增公告',
            'icon'  => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href'  => url('add'),
        ];
        $btn_access2 = [
            'title' => '删除公告',
            'icon'  => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('delete', ['id' => '__id__']),
        ];
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumn('id', '编号', 'text')
            ->addColumn('title', '标题', 'text')
            ->addColumn('img', '封面', 'picture')
            ->addColumn('created_time', '发布日期', 'datetime','','Y-m-d H:i:s')
            ->addColumn('updated_time', '编辑日期', 'datetime','','Y-m-d H:i:s')
            ->addColumn ('status', '状态', 'status','',[1=>'显示:success', 2=>'不显示:danger'])
            ->addColumn ('type', '类型', 'status','',[1=>'通告:success', 2=>'系统消息:danger'])
            ->addColumn('right_button','操作','btn')
            ->setColumnWidth([
                'id'  => 100,
                'title' => 200,
                'status' => 200,
                'is_top'=>200,
                'create_time'=> 200,
                'update_time'=> 200,
                'right_button'=>200
            ])
            ->setTableName('qt_notic') // 指定数据表名
            ->addTopButton('custom',$btn_access) // 添加顶部按钮
            ->addRightButton('edit',$btn_access1) // 添加右侧按钮
            ->addRightButton('delete',$btn_access2) // 添加右侧按钮
            ->addOrder('id,title,create_time,update_time')
            ->setPrimaryKey('id') // 设置主键名为username
            ->setPageTitle('公告列表')
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->fetch();
    }
    //添加公告
    public function add(){
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            //var_dump($data);die;
            $result = $this->validate($data, 'Notic');
            if(true !== $result) $this->error($result);
            $data['created_time'] = time();
            $page = NoticModel::create($data);
//            $userRes = Db::name('qt_users')->column('id');
//            //dump($userRes);die;
//            foreach ($userRes  as $key =>$value){
//                $insert[$key]['notic_id'] =  $page->id;
//                $insert[$key]['status'] =  1;
//                $insert[$key]['user_id'] =  $value;
//            }
            if ($page) {
                $this->success('新增成功', 'notic_list');
            } else {
                $this->error('新增失败');
            }
        }else
        {
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7','title','标题'],
                        ['textarea:7','desc','描述'],
                        ['image', 'img', '封面图片'],
                        ['Ueditor:7','content','内容'],
                        ['radio', 'type', '类型', '', [1=>'通告', 2=>'系统消息'],'1'],
                        ['radio', 'status', '状态', '', [1=>'显示', 2=>'不显示'],'1']
                    ]
                )
                ->setPageTitle('添加公告')
                ->fetch();
        }
    }

    //编辑公告
    public function edit($id=null){
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Notic');
            if(true !== $result) $this->error($result);

            if (NoticModel::update($data)) {
                $this->success('编辑成功', 'notic_list');
            } else {
                $this->error('编辑失败');
            }
        }else
        {
            $info = NoticModel::get($id);
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden','id','id'],
                        ['text:7','title','标题'],
                        ['textarea:7','desc','描述'],
                        ['image', 'img', '封面图片'],
                        ['Ueditor:7','content','公告内容'],
                        ['radio', 'type', '类型', '', [1=>'通告', 2=>'系统消息'],'1'],
                        ['radio', 'status', '状态', '', [1=>'显示', 2=>'不显示'],'1']
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('编辑公告')
                ->fetch();
        }
    }
    //删除公告
    public function delete($id=null){
        if ($id === null) $this->error('缺少参数');
        $re=NoticModel::destroy($id);
        if($re){
            $this->success('删除成功','notic_list');
        }else{
            $this->error('系统繁忙请稍后再试','notic_list');
        }

    }



}